﻿using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CLDataAppManagement;
using CLVisionMQTT;
using NationalInstruments.TestStand.Interop.API;
using Newtonsoft.Json.Linq;

namespace CLVisionEICS
{
    public class CLEICSClient : CLEICSClientBase
    {
        public override JObject eicsJson { get; set; }
        public CLEICSClient()
        {

        }

        public override bool IntMqttClient(string server, int port, string username, string password, string topicName, string clientId, out int ErrorCode, out string ErrorText, string cdatFilePath = null)
        {
            ErrorCode = 0;
            ErrorText = $"{nameof(CLEICSClient)}:{nameof(IntMqttClient)}: Start!";
            try
            {
                base.mQTTClient = new MQTTClient();
                base.mQTTClient.EventReceivedMessage = base.EventReceivedMessage;

                base.mQTTClient.EventDisconnected = base.EventDisconnected;
                bool ret = mQTTClient.IntMqttClient(server, port, username, password, topicName, clientId, out ErrorCode, out ErrorText);

                if (ErrorCode == -1)
                {
                    ErrorText = $"{nameof(CLEICSClient)}:{nameof(IntMqttClient)}: Fail! " + ErrorText;
                    return false;
                }
                ProcessEICSData processEICSData = new ProcessEICSData();
                eicsJson = new JObject();
                if (cdatFilePath == null)
                {
                    string jsonStr = processEICSData.EntityToJSON<EICSRoot>(new EICSRoot());
                    eicsJson = JObject.Parse(jsonStr);
                }
                else
                {
                    JToken jtoken = processEICSData.CdatToEICSJson(cdatFilePath, out ErrorCode, out ErrorText);
                    eicsJson = JObject.Parse(jtoken.ToString());
                    if (ErrorCode == -1) return false;
                }

                return true;
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = $"{nameof(CLEICSClient)}:{nameof(IntMqttClient)}: Fail! " + ex.Message;
                return false;
            }
        }

        public override bool SetEICSValueByArrayString(string value, out int ErrorCode, out string ErrorText)
        {
            ErrorCode = 0;
            ErrorText = $"{nameof(CLEICSClient)}:{nameof(SetEICSValueByArrayString)}: Start!";
            try
            {
                JObject job = JObject.Parse(value);
                eicsJson["PcsDetails"] = job["PcsDetails"];
                return true;
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = $"{nameof(CLEICSClient)}:{nameof(SetEICSValueByArrayString)}: Fail! " + ex.Message;
                return false;
            }
        }

        public override bool SetEICSValueByKey(string key, object value, out int ErrorCode, out string ErrorText)
        {
            ErrorCode = 0;
            ErrorText = $"{nameof(CLEICSClient)}:{nameof(SetEICSValueByKey)}: Start!";
            try
            {
                eicsJson[key] = JObject.FromObject(value);
                return true;
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = $"{nameof(CLEICSClient)}:{nameof(SetEICSValueByKey)}: Fail! " + ex.Message;
                return false;
            }
        }

        public override bool SetEICSValueByKey(PropertyObject key, out int ErrorCode, out string ErrorText)
        {
            ErrorCode = 0;
            ErrorText = $"{nameof(CLEICSClient)}:{nameof(SetEICSValueByKey)}: Start!";
            try
            {
                switch (key.Type.ValueType)
                {
                    case PropertyValueTypes.PropValType_String:
                        string stringValue = key.GetValString("", 0);
                        eicsJson[key.Name] = JObject.FromObject(stringValue);
                        break;
                    case PropertyValueTypes.PropValType_Boolean:
                        bool booleanValue = key.GetValBoolean("", 0);
                        eicsJson[key.Name] = JObject.FromObject(booleanValue);
                        break;
                    case PropertyValueTypes.PropValType_Number:
                        double doubleValue = key.GetValNumber("", 0);
                        eicsJson[key.Name] = JObject.FromObject(doubleValue);
                        break;
                    default:
                        break;
                }
                return true;
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = $"{nameof(CLEICSClient)}:{nameof(SetEICSValueByKey)}: Fail! " + ex.Message;
                return false;
            }
        }

        public override bool SetEICSValuesByKeys(PropertyObject key, out int ErrorCode, out string ErrorText)
        {
            ErrorCode = 0;
            ErrorText = $"{nameof(CLEICSClient)}:{nameof(SetEICSValuesByKeys)}: Start!";
            try
            {
                PropertyObject[] propertyObjects = key.GetSubProperties("", 0);
                foreach (var item in propertyObjects)
                {
                    switch (item.Type.ValueType)
                    {
                        case PropertyValueTypes.PropValType_String:
                            string stringValue = item.GetValString("", 0);
                            eicsJson[item.Name] = JToken.FromObject(stringValue);
                            break;
                        case PropertyValueTypes.PropValType_Boolean:
                            bool booleanValue = item.GetValBoolean("", 0);
                            eicsJson[item.Name] = JToken.FromObject(booleanValue);
                            break;
                        case PropertyValueTypes.PropValType_Number:
                            double doubleValue = item.GetValNumber("", 0);
                            eicsJson[item.Name] = JToken.FromObject(doubleValue);
                            break;
                        default:
                            break;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = $"{nameof(CLEICSClient)}:{nameof(SetEICSValuesByKeys)}: Fail! " + ex.Message;
                return false;
            }
        }
    }
}
